package com.lib.framework.utils

import android.app.ActivityManager
import android.content.Context
import android.os.Process

/**
 * Copyright (C), 2019-2019
 * FileName: AndroidUtil
 * Author: Kevin
 * Date: 2019-12-31 11:30
 * Description: android 工具类
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class AndroidUtil {

    companion object {

        /**
         * 是否是主进程
         */
        fun isMainProcess(context: Context): Boolean {
            return context.packageName == getCurProcessName(context)
        }


        /**
         * 获得当前进程的名字
         *
         * @return 进程号
         */
        fun getCurProcessName(context: Context): String? {
            val pid = Process.myPid()
            val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as? ActivityManager
            if (activityManager != null && activityManager.runningAppProcesses != null) {
                for (appProcess in activityManager
                    .runningAppProcesses) {
                    if (appProcess.pid == pid) {
                        return appProcess.processName
                    }
                }
            }
            return ""
        }
    }
}